package com.miaoji.controller;

import com.miaoji.domain.po.Cloth;
import com.miaoji.service.ClothCategoryService;
import com.miaoji.service.ClothService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/cloth")
public class ClothManagementController {
    @Autowired
    private ClothService clothService;
    @Autowired
    ClothCategoryService categoryService;
    //首页初始化查询默认1
    @GetMapping("/init")
    public List ClothGetInit(){
        return clothService.ClothSelect();
    }
    //查询接口,number为页数
    @GetMapping("/{number}")
    public List ClothGetPage(@PathVariable int number){
        return clothService.ClothSelect(number);
    }
    //条件查询接口
    @PostMapping("/search")
    public List ClothSearch(@RequestBody Cloth cloth){
        return clothService.ClothSelect(cloth);
    }
    @PostMapping("/insert")
    public Integer ClothInsert(@RequestBody Cloth cloth){
        LocalDateTime currentTime = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = currentTime.format(dateTimeFormatter);
        LocalDateTime parsedTime = LocalDateTime.parse(format, dateTimeFormatter);
        cloth.setCreateTime(parsedTime);
        cloth.setUpdateTime(parsedTime);
        Integer i = clothService.ClothInsert(cloth);
        return i;
    }
    @PostMapping("/update")
    public void ClothUpdate(@RequestBody Cloth cloth){
        clothService.ClothUpdate(cloth);
        categoryService.categoryTreUpdate(cloth.getId(),cloth.getClothCategoryId()==1?"男":"女");
    }
    @PostMapping("/delete")
    public void ClothDelete(@RequestBody Cloth cloth){
        clothService.ClothDelete(cloth);
    }


}
